define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {

	$scope.menu = {};

	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.find();
	});

	if(params.father_id!='undefined'){
		httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
			if("0000" === data.code){
				$scope.menu_name = data.data[0].name;
				$scope.findButton(data.data[0].id);
			}else{
				alert(data.msg);
			}
		});
	}else {
		$scope.form.type=params.type;
	}

	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//按钮事件
	$scope.operate = function(event) {
		$scope[event]();
	}

	$scope.add = function (){
		$scope.menu.url = "modules/content/schoolroll/student/add?fk_menuid="+params.fk_menuid+ "&father_id=" + params.father_id;
		if(params.father_id!='undefined'){
			eventBusService.publish("modules.welcome.load", $scope.menu);
		}else {
			eventBusService.publish("modules.content.load", $scope.menu);
		}
	}

	$scope.info = function (){
		var values = [];
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择查看数据。"});
		}else if(values.length > 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
		}else {
			$scope.menu.url = "modules/content/schoolroll/student/info?id=" + values[0] + "&father_id=" + params.father_id;
			if(params.father_id!='undefined'){
				eventBusService.publish("modules.welcome.load", $scope.menu);
			}else {
				eventBusService.publish("modules.content.load", $scope.menu);
			}
		}
	}

	$scope.update = function (){
		var values = [];
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
		}else if(values.length > 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
		}else {
			$scope.menu.url = "modules/content/schoolroll/student/update?id=" + values[0] + "&father_id=" + params.father_id+ "&sheet=" + params.sheet+ "&field=" + params.field+ "&type=" + params.type;
			if(params.father_id!='undefined'){
				eventBusService.publish("modules.welcome.load", $scope.menu);
			}else {
				eventBusService.publish("modules.content.load", $scope.menu);
			}
		}
	}

	$scope.delete = function (){
		var values = [];
		var ids="";
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			var arr=$(this).val().split(',');
			values.push(arr[0]);
			ids = ids+",'"+arr[0]+"'";
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择删除的数据。"});
		}else{
			eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"数据？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.user_delete,{"id":ids.replace(',','')},function(data){
						if("0000" === data.code){
							httpService.post(app.urls.student_delete,{"id":ids.replace(',','')}, function(data) {
								if ("0000" === data.code) {
									httpService.post(app.urls.error_delete,{"record":ids.replace(',','')}, function(data) {
										if ("0000" === data.code) {
											$scope.select();
										} else {
											alert(data.msg);
										}
									});
								} else {
									alert(data.msg);
								}
							});
						}else{
							alert(data.msg);
						}
					});
				},"cancel":function(){
					//用户取消，不做操作
				}});
		}
	}

	$scope.rollin = function (){
		eventBusService.publish("modal.page","modules/content/schoolroll/student/transfer?v="+Math.random()+"&FatherControllerName="+$scope.controllerName+"&type=1");
	}

	$scope.rollout = function (){
		var values = [];
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择数据。"});
		}else if(values.length > 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
		}else {
			let item = $scope.rows.find((item)=>{
				return item.id == values[0]
			})
			if (item.status.includes('转出')){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请勿重复操作。"});
				return;
			}
			eventBusService.publish("modal.page","modules/content/schoolroll/student/transfer?v="+Math.random()+"&FatherControllerName="+$scope.controllerName+"&type=2&id="+values[0]);
		}
	}

	$scope.stop = function (){
		let status = $scope.statusList.find(item =>{
			return item.name.includes('休学');
		});

		var values = [];

		// fk_student
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择数据。"});
		}else if(values.length > 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
		}else {
			let item = $scope.rows.find((item)=>{
				return item.id == values[0]
			})
			if (item.status.includes('休学')){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请勿重复操作。"});
				return;
			}
			if (item.status.includes('转出')){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"无法操作，该学生已转出!"});
				return;
			}
			let query = {
				'fk_student': item.id,
				'name': item.name,
				'sfzjh': item.sfzjh,
				'type': '3',
				'fk_unit': $scope.form.fk_unit,
				'fk_grade': item.fk_grade,
				'fk_class': item.fk_class,
				'fk_last_state': item.state,
				'state':status.id
			}

			eventBusService.publish("modal.alert",{"content":"请是否确认对该学生进行休学操作？","isAutoClose":false,"confirm":function(){
				httpService.post(app.urls.saveTransferInfo,query,function(data){
					if("0000" === data.code){
						$scope.find();
					}else{
						alert(data.msg);
					}
				});
			},"cancel":function(){
				//用户取消，不做操作
			}});
		}
	}

	$scope.review = function (){
		var values = [];
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择数据。"});
		}else if(values.length > 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
		}else {
			let item = $scope.rows.find((item)=>{
				return item.id == values[0]
			})
			if (item.status.includes('复学')){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请勿重复操作。"});
				return;
			}
			if (item.status.includes('转出')){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"无法操作，该学生已转出!"});
				return;
			}
			if (item.status.includes('休学')){
				eventBusService.publish("modal.page","modules/content/schoolroll/student/transfer?v="+Math.random()+"&FatherControllerName="+$scope.controllerName+"&type=4&id="+values[0]);
			}else{
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择休学状态的学生进行复学。"});
			}
		}
	}

	$scope.upgrade = function (){
		eventBusService.publish("modal.alert",{"content":"你是否确认对整个学校的年级进行【升级/学】操作？","isAutoClose":false,"confirm":function(){
			httpService.post(app.urls.upgrade, {'fk_unit': $scope.form.fk_unit},function(data){
				if("0000" === data.code){
					// var m2 = {"url": "modules/content/schoolroll/upgrade/list?FatherControllerName="+$scope.controllerName};
					// eventBusService.publish("modules.content.load",m2);
					// eventBusService.publish("modal.alert", {"content": "升级/学成功。请在【升学管理】进行最终确认！"});
					alert("升级/学成功。请在【升学管理】进行最终确认！");
				}else if("4000" === data.code) {
					alert("无法进行【升级/学】操作：无可操作年级！");
				}else {
					alert(data.msg);
				}
			});
		},"cancel":function(){
			//用户取消，不做操作
		}});
	}

	//按钮点击事件-条件查询
	$scope.select = function(){
		$scope.page.current = 1;
		$scope.find();
	}

	//查询学生列表
	$scope.find = function(){
		httpService.postpage(app.urls.findSchoolStudent,$scope.form,function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//查询年级
	$scope.findGrade = function(){
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//获取班级
	$scope.findClass=function(){
		$scope.classList = [];
		httpService.post(app.urls.class_selectClassName,{"fk_unit":$scope.form.fk_unit,"fk_grade":$scope.form.fk_grade,"status":'0'},function(data){
			if("0000" === data.code){
				$scope.classList = data.data;
			}else{
				alert(data.msg);
			}
			$scope.$applyAsync();
		});
	}

	//学生状态
	$scope.findStudentStatus = function(){
		httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"SJAAAAA10061"},function(data){
			if("0000" === data.code){
				$scope.statusList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	let init = function (){
		$scope.form.fk_unit=app.userInfo.unitId;
		$scope.find();
		$scope.findGrade();
		$scope.findStudentStatus();
	}

	init();
//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});